/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.apps.utils;

import com.apps.beans.AccountBean;
import com.apps.controllers.ScpFromController;
import com.apps.controllers.ShellController;
import com.apps.ui.FileDownloadUI;
import com.apps.ui.SearchResultScreen;
import java.util.ArrayList;
import javax.swing.JTextArea;

/**
 *
 * @author ARIJIT PAL
 */
public class SearchHelper implements Runnable{

    ShellController shell;
    AccountBean account;
    String searchText;
    int timeOut;
    String localDir;
    //UI
    SearchResultScreen searchResultScreen;
    
    public SearchHelper(String localDir, ShellController shell, AccountBean account, String searchText, int timeOut){
        this.shell = shell;
        this.account = account;
        this.searchText = searchText;
        this.timeOut = timeOut;
        this.localDir = localDir;
    }
    @Override
    public void run(){
        try {
            String remoteScript = account.getPath();
            searchText = searchText.replace("\"", "\\\"");
            remoteScript += " \"" + searchText + "\" ";
            String output = "";
            if (shell.getShellStatus()) {
                output = shell.executeCmdRT(remoteScript, "##$##END#SCRIPT##$##", timeOut);
            }
            setStatus(output, account);
        } catch (Exception ex) {
            setStatus("ERROR: Executing script in '"+account.getHost()+"' : "+ex.getMessage(),account);
        }
    }

    public void setStatus(String output, AccountBean account){
        System.out.println("\n\n\n<\n"+output+"\n>\n\n\n");
        //START: Search UI
         if(searchResultScreen != null){
             searchResultScreen.getProgressBar().setIndeterminate(false);
             int value = (int)(searchResultScreen.getProgressBar().getValue()+(1/searchResultScreen.getSearchCount())*100);
             searchResultScreen.getProgressBar().setValue(value);
             searchResultScreen.getProgressBar().setString("Done");
         }
        //END
        //Output message/file search
        ArrayList<String> outList = new ArrayList<String>();
        String[] lines = output.split("\n");
        for(String data : lines){
            if(data.contains("Message Output: ")){
                outList.add(data);
                if(searchResultScreen != null){
                    searchResultScreen.getOutputMsgTextBox().append(
                             "\n===================================\n"
                            +account.getHost()
                            +"\n===================================\n"
                            +data
                            +"\n===================================\n"
                            );
                }
            }else if(data.contains("File Output: <>")){
                outList.add(data);
                String rFileName = null;
                String[] strAry = data.split("<>");
                for(int count = 0 ; count < strAry.length ; count++){
                    if(strAry[count].contains("File Output:")){
                        if((count+1)<strAry.length)
                            rFileName = strAry[count+1];
                            rFileName = rFileName.trim();
                            rFileName = rFileName.replace("\n", "");
                    }
                }
                String lFileName = localDir+"/"+account.getHost()+"/"+rFileName;
                if(searchResultScreen != null && (rFileName != null && rFileName.length()>0)){
                    searchResultScreen.getOutputMsgTextBox().append(
                             "\n===================================\n"
                            +account.getHost()
                            +"\n===================================\n"
                            +data
                            +"\n===================================\n"
                            );
                    //SCP controller
                    ScpFromController scpFromController = new ScpFromController();
                    FileDownloadUI uiObject = new FileDownloadUI();
                    scpFromController.setFileDownloadUI(uiObject);
                    scpFromController.setHost(account.getHost());
                    scpFromController.setPort(account.getPort());
                    scpFromController.setUser(account.getUserName());
                    scpFromController.setPasswd(account.getPassword());
                    scpFromController.setFileName(rFileName);
                    scpFromController.setSaveAsLocal(lFileName);
                    searchResultScreen.getOutputFilePane().add(uiObject);
                    searchResultScreen.getOutputFilePane().updateUI();
                    searchResultScreen.getOutputFilePane().repaint();
                    Thread th = new Thread(scpFromController);
                    th.start();
                }
            }else{
                if(data.contains("Error")||data.contains("error")){
                    if(searchResultScreen != null){
                        searchResultScreen.getOutputMsgTextBox().append(
                                 "\n===================================\n"
                                +account.getHost()
                                +"\n===================================\n"
                                +data
                                +"\n===================================\n"
                                );
                    }
                }
            }
        }
    }
    public void setUI(SearchResultScreen searchResultScreen){
        this.searchResultScreen = searchResultScreen;
    }
}
